package com.xyj.service.impl;

import com.xyj.dao.MenuDaoI;
import com.xyj.dao.UserDaoI;
import com.xyj.domain.TMenu;
import com.xyj.domain.TUser;
import com.xyj.service.RepairServiceI;
import com.xyj.util.Encrypt;
import com.xyj.util.LoggerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 徐宇健 on 2015/2/12 0012.
 */
@Service("repairService")
public class RepairServiceImpl implements RepairServiceI {
    private MenuDaoI menuDao;
    private UserDaoI userDao;

    public UserDaoI getUserDao() {
        return userDao;
    }
    @Autowired
    public void setUserDao(UserDaoI userDao) {
        this.userDao = userDao;
    }

    public MenuDaoI getMenuDao() {
        return menuDao;
    }
    @Autowired
    public void setMenuDao(MenuDaoI menuDao) {
        this.menuDao = menuDao;
    }

    @Override
    public void repair() {
        LoggerUtil.getLogger().info("开始修复数据>>>>>:");
        repairMenu();
        repairUer();
        LoggerUtil.getLogger().info("修复完毕>>>>>:");

    }

    private void repairUer() {
        Map ops = new HashMap();
        ops.put("name","admin");
        TUser tUser =    userDao.get("from TUser t where t.name = :name  and tid != '0'", ops);
        if(tUser !=null){
          LoggerUtil.getLogger().info("删除用户非法信息>>>>>:");
           userDao.delete(tUser);
        }

        TUser admin = new TUser();
        admin.setTid("0");
        admin.setName("admin");
        admin.setPwd(Encrypt.e("123456"));
        userDao.saveOrUpdate(admin);
            LoggerUtil.getLogger().info("初始化最高权限用户'admin'成功;密码为:123456 >>>>>:");
    }

    private void repairMenu() {
        //TODO

        LoggerUtil.getLogger().info("初始化菜单数据>>>>>:");
        TMenu rootMenu = new TMenu();
        rootMenu.setId("0");
        rootMenu.setName("首页");
        menuDao.saveOrUpdate(rootMenu);

        TMenu xtgl = new TMenu();
        xtgl.setId("xtgl");
        xtgl.setName("系统管理");
        xtgl.settMenuByTid(rootMenu);
        menuDao.saveOrUpdate(xtgl);


        TMenu yhgl = new TMenu();
        yhgl.setId("yhgl");
        yhgl.setName("用户管理");
        yhgl.settMenuByTid(xtgl);
        menuDao.saveOrUpdate(yhgl);

        TMenu jsgl = new TMenu();
        jsgl.setId("jsgl");
        jsgl.setName("角色管理");
        jsgl.settMenuByTid(xtgl);
        menuDao.saveOrUpdate(jsgl);


        TMenu qxgl = new TMenu();
        qxgl.setId("qxgl");
        qxgl.setName("权限管理");
        qxgl.settMenuByTid(xtgl);
        menuDao.saveOrUpdate(qxgl);

        TMenu cdgl = new TMenu();
        cdgl.setId("cdgl");
        cdgl.setName("菜单管理");
        cdgl.settMenuByTid(xtgl);
        menuDao.saveOrUpdate(cdgl);

        TMenu buggl = new TMenu();
        buggl.setId("buggl");
        buggl.setName("BUG管理");
        buggl.settMenuByTid(xtgl);
        buggl.setUrl("/adminMenu/buggl.jsp");
        menuDao.saveOrUpdate(buggl);

        LoggerUtil.getLogger().info("初始化菜单完毕>>>>>:");

    }
}
